#! /bin/bash
number_of_clients=$(tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep -c "^V")
if [[ "$number_of_clients" = 0 ]]; then
  echo "There are no existing clients!"
  exit
fi
tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | nl -s ') '
read -p "Client: " client_number
until [[ "$client_number" =~ ^[0-9]+$ && "$client_number" -le "$number_of_clients" ]]; do
  echo "$client_number: invalid selection."
  read -p "Client: " client_number
done
client=$(tail -n +2 /etc/openvpn/server/easy-rsa/pki/index.txt | grep "^V" | cut -d '=' -f 2 | sed -n "$client_number"p)
read -p "Confirm $client revocation? [y/N]: " revoke
until [[ "$revoke" =~ ^[yYnN]*$ ]]; do
  echo "$revoke: invalid selection."
  read -p "Confirm $client revocation? [y/N]: " revoke
done
if [[ "$revoke" =~ ^[yY]$ ]]; then
  cd /etc/openvpn/server/easy-rsa/
  ./easyrsa --batch revoke "$client"
  ./easyrsa --batch --days=3650 gen-crl
  rm -f /etc/openvpn/server/crl.pem
  cp /etc/openvpn/server/easy-rsa/pki/crl.pem /etc/openvpn/server/crl.pem
  chown nobody:nogroup /etc/openvpn/server/crl.pem
  sed -i "/$client/d" /etc/openvpn/server/userlist.txt
  echo "$client revoked!"
else
  echo "$client revocation aborted!"
fi
exit
